
import { Injectable } from '@angular/core';
import { Router, Resolve, ActivatedRouteSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { DepartmentService } from './departmentService';
import { Department } from '../models/Department';

@Injectable()
export class DepartmentsResolveService implements Resolve<{total: number, departments: Department[]}> {

    constructor(private departmentService: DepartmentService) {}

    resolve(route: ActivatedRouteSnapshot): Observable<{total: number, departments: Department[]}>  {

        return this.departmentService.getDepartments().pipe(map(res => {
          if (res.success) {
            return  {total: res.total, departments: res.res};
          } else {
            console.error(res.res);
            return {total: 0, departments: []};
          }

        }));
    }
}

